package com.cuitrip.component.page;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.cuitrip.service.R;
import com.lab.app.BaseActivity;
import com.lab.app.BaseFragment;
import com.lab.jumper.BundleCenter;
import com.lab.jumper.FragmentJumper;
import com.lab.jumper.Jumper;


/**
 * Created by AK
 * 通用型Activity <br>
 * 主要用于承载Fragment,而无需再次创建单独Activity,目前只支持Standard launchMode,适用于简单业务逻辑Fragment
 */
public class SimpleEmptyFragmentActivity extends BaseActivity {

    public static final String PARAM_FRAGMENT_CLASS = "param_fragment_class";
    public static final String PARAM_BUNDLE_ARGS = "param_bundle_args";

    private Class<? extends BaseFragment> mFragmentClass;
    private Bundle mFragmentBundle;

    

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 移除BunderCenter原始Bundle,这里待优化,可能因业务需要而采取原始数据作为后续Bundle传入Fragment
        BundleCenter.getInstance().removeBundle(getIntent());
        if (null != getIntent()) {
            mFragmentClass = (Class<? extends BaseFragment>) getIntent().getSerializableExtra(PARAM_FRAGMENT_CLASS);
            mFragmentBundle = getIntent().getBundleExtra(PARAM_BUNDLE_ARGS);
        }
        // 提供的FragmentClass无效
        if (null == mFragmentClass) {
            Jumper.finishActivity(this);
            return;
        }
        setContentView(R.layout.page_container_layout);
        FragmentJumper.addFragment(getCustomSelfFragmentManager(), this, R.id.container, mFragmentClass, mFragmentBundle, null, false);
    }

    public static void startFragmentActivityFromFragment(BaseFragment fragment, Class<? extends BaseFragment> fragmentClass, Bundle args) {
        if (null != fragment && null != fragmentClass) {
            Intent intent = new Intent(fragment.getActivity(), SimpleEmptyFragmentActivity.class);
            intent.putExtra(PARAM_FRAGMENT_CLASS, fragmentClass);
            if (null != args) intent.putExtra(PARAM_BUNDLE_ARGS, args);
            Jumper.launchActivity(fragment.getHostActivity(), intent);
        }
    }

    public static void startFragmentActivity(Context context, Class<? extends BaseFragment> fragmentClass, Bundle args) {
        if (null != context && null != fragmentClass) {
            Intent intent = new Intent(context, SimpleEmptyFragmentActivity.class);
            intent.putExtra(PARAM_FRAGMENT_CLASS, fragmentClass);
            if (null != args) intent.putExtra(PARAM_BUNDLE_ARGS, args);
            Jumper.launchActivity(context, intent);
        }
    }

    public static void startFragmentActivityForResult(Activity activity, Class<? extends BaseFragment> fragmentClass, Bundle args, int requestCode) {
        if (null != activity && null != fragmentClass) {
            Intent intent = new Intent(activity, SimpleEmptyFragmentActivity.class);
            intent.putExtra(PARAM_FRAGMENT_CLASS, fragmentClass);
            if (null != args) intent.putExtra(PARAM_BUNDLE_ARGS, args);
            Jumper.launchActivityForResult(activity, intent,requestCode);
        }
    }
}
